$(function() {
	FastClick.attach(document.body);
});

var statisticsProjectVue = new Vue({
	el: "body",
	data: {
		mapData: [],
		indexNum: "",
		statrYear: "",
		startWeek: "",
		endYear: "",
		endWeek: "",
		nowPage: 0, //保存当前加载的页数
		loadMore: false, //控制显示加载更多信息
		totalNum: 0, //当前信息总量
		noMore: false, //控制显示已无更多数据信息
		loading: false //控制在下拉一次未完成之间不再执行下拉
	},
	methods: {
		heTong: function() {
			$(".statistics-map-tabbar>div").eq(1).removeClass("forbidden")
			$(".statistics-map-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			_this.indexNum = 1;
			searchMap(_this, "签订合同")
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-map-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		chengGuo: function() {
			$(".statistics-map-tabbar>div").eq(1).removeClass("forbidden")
			$(".statistics-map-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			_this.indexNum = 2;
			searchMap(_this, "交付成果")
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-map-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		chanZhi: function() {
			$(".statistics-map-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			$(".statistics-map-tabbar>div").eq(1).addClass("forbidden")
			_this.indexNum = 3
			searchMapProduct(_this)
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-map-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		back: function() {
			window.history.go(-1); //返回上一页
		}
	},
	created: function() {
		_this = this; //防止出现闭包
		_this.startYear = getUrlItem("startYear");
		_this.startWeek = getUrlItem("startWeek");
		_this.endYear = getUrlItem("endYear");
		_this.endWeek = getUrlItem("endWeek");
		
		searchMapChart(_this);

		//判断是否需要下拉刷新
		$(document.body).infinite().on("infinite", function() {
			//一页显示10条数据,因此当列表中数据大于等于10时才可以进行下拉刷新
			if(_this.mapData.length >= 12) {
				//防止多次进行刷新重复操作
				if(_this.loading) return;
				_this.loading = true;
				_this.loadMore = true;
				var beClick = $(".statistics-map-title .tab .active").html(); //判断当前点击的是下方是下方三个tab的哪一个
				console.log(beClick)
				setTimeout(function() {
					var canPage = Math.ceil(_this.totalNum / 12)
					//	当前页大于分页总页则显示已无更多数据
					switch(beClick) {
						case "签订合同":
							if(canPage > _this.nowPage) {
								searchMoreMap(_this, "签订合同")
							} else {
								_this.noMore = true;
								_this.loadMore = false;
								_this.loading = true;
							};
							break;
						case "交付成果":
							if(canPage > _this.nowPage) {
								searchMoreMap(_this, "交付成果")
							} else {
								_this.noMore = true;
								_this.loadMore = false;
								_this.loading = true;
							};
							break;
						case "季度产值":
							if(canPage > _this.nowPage) {
								searchMoreMapProduct(_this)
							} else {
								_this.noMore = true;
								_this.loadMore = false;
								_this.loading = true;
							}
							break;
					};
				}, 2000);
			} else {
				if(_this.mapData.length == 0) {

				} else {
					_this.noMore = true
				}
			}
		});

	},
	ready: function() {
		$(".statistics-map-title .tab>div").each(function(i) {
			$(".statistics-map-title .tab>div").eq(i).click(function() {
				$(".statistics-map-title .tab div").removeClass("active")
				$(this).addClass("active")
			})
		})
		//顶部tab切换

		$(".statistics-map-tabbar>div").eq(1).click(function() {
			var beclick = $(".statistics-map-title  .active").html()
			if(beclick != "总体款项") {
				$(".statistics-map-tabbar>div").removeClass("active")
				$(this).addClass("active")
				$(".statistics-map-body").hide()
				$(".statistics-map-title").addClass("hidden")
				$(".statistics-map-cart").removeClass("hidden")
			} else {

			}
		})

		$(".statistics-map-tabbar>div").eq(0).click(function() {
			$(".statistics-map-tabbar>div").removeClass("active")
			$(this).addClass("active")
			$(".statistics-map-body").show()
			$(".statistics-map-title").removeClass("hidden")
			$(".statistics-map-cart").addClass("hidden")
		})

		$(function() {
			setFullScreen(".statistics-map-cart", 50)
			var type = getUrlItem("type");
			if(type == "hetong") {
				$(".statistics-map-title .tab>div").eq(0).click()
			}
			if(type == "chengguo") {
				$(".statistics-map-title .tab>div").eq(1).click()
			}
			if(type == "chanzhi") {
				$(".statistics-map-title .tab>div").eq(2).click()
			}
			$(".statistics-map-tabbar>div").eq(0).click()
		})
	}
})

function searchMap(a, b) {
	a.mapData = [] //切换tab前先清空列表数据防止残留数据触发加载更多操作
	a.nowPage = 1;
	a.noMore = false;
	a.loading = false;
	a.loadMore = false;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getGraphProductList?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"curcode": b,
			"pagesize": 12,
			"pageno": a.nowPage
		},
		dataType: "json",
		success: function(jsondata) {
			console.log(jsondata)
			jsondata.data.forEach(function(item) {
				if(item.CHTYPE != null) {
					item.CTYPE = item.CHTYPE.split(",") //解决返回时间型带T字符
				}
			});
			a.totalNum = jsondata.total_count
			a.mapData = jsondata.data
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}

function searchMoreMap(a, b) {
	a.nowPage++;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getGraphProductList?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"curcode": b,
			"pagesize": 12,
			"pageno": a.nowPage
		},
		dataType: "json",
		success: function(jsondata) {
			console.log(jsondata)
			jsondata.data.forEach(function(item) {
				if(item.CHTYPE != null) {
					item.CTYPE = item.CHTYPE.split(",") //解决返回时间型带T字符
				}
			});
			for(var i = 0; i < jsondata.data.length; i++) {
				a.mapData.push(jsondata.data[i]);
			}
			a.loadMore = false;
			a.loading = false;
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}

function searchMapProduct(a) {
	a.mapData = [] //切换tab前先清空列表数据防止残留数据触发加载更多操作
	a.nowPage = 1;
	a.noMore = false;
	a.loading = false;
	a.loadMore = false;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getFoundsList?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"pagesize": 12,
			"pageno": a.nowPage
		},
		dataType: "json",
		success: function(jsondata) {
			console.log(jsondata)
			a.totalNum = jsondata.total_count
			a.mapData = jsondata.data
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function searchMoreMapProduct(a) {
	a.nowPage++;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getFoundsList?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"pagesize": 12,
			"pageno": a.nowPage
		},
		dataType: "json",
		success: function(jsondata) {
			for(var i = 0; i < jsondata.data.length; i++) {
				a.mapData.push(jsondata.data[i]);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function getUrlItem(name) { //id
	var url = location.search
	start = url.indexOf(name + "=")
	if(start != -1) {
		start = start + name.length + 1
		end = url.indexOf("&", start)
		if(end == -1) {
			end = url.length
			return unescape(url.substring(start, end))
		}
		return unescape(url.substring(start, end))
	}
	return '';
}

function searchMapChart(a) {
	var x = []; //X轴
	var y = []; //Y轴
	var xtext = []; //X轴TEXT
	var color = ["#FCB954", "#25AA1A", "#F8863A", "#04A9F5"];
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getGraphProductSum ?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			console.log(jsondata)
			var jsonList = []
			var s = 1;
			for(var item in jsondata) {
				if(item == "总体款项") {} else {
					var key = {
						TYPE: item,
						NUM: jsondata[item]
					}
					jsonList.push(key)
				}
			}
			for(var key in jsonList) {
				jsonList[key].y = jsonList[key].NUM; //给Y轴赋值
				xtext[key] = jsonList[key].TYPE; //给X轴TEXT赋值
				jsonList[key].color = color[key];
			}
			chart.series[0].setData(jsonList); //数据填充到highcharts上面
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
	var chart = new Highcharts.Chart({
		chart: {
			renderTo: 'mapCart',
			type: 'column' //显示类型 柱形
		},
		title: {
			text: '' //图表的标题
		},
		//当鼠标经过时的提示设置
		tooltip: {
			headerFormat: '<span style="font-size:11px"><b>类别：<b></span><br>',
			pointFormat: '<span style="color:{point.color}">{point.TYPE}</span><br><b>{point.y:.1f} 个</b><br/>'
		},
		xAxis: {
			categories: xtext
		},
		yAxis: {
			title: {
				text: '', //Y轴的名称
				style: {
					color: '#333333',
					fontWeight: 'bold',
					fontSize: '14px'
				}
			}
		},
		series: [{
			name: "所处环节"
		}],
		plotOptions: { //plotOptions全局设置
			series: { //对series的全局设置                        
				cursor: 'pointer',
				dataLabels: {
					enabled: true, //是否在节点上显示数据
					color: '#333',
					align: 'center',
					style: { fontSize: '13px' }
				}
			}
		},
		credits: {
			enabled: false //隐藏右下角网址
		},
		exporting: {
			enabled: false //隐藏右上角打印
		},
		legend: {
			enabled: false //去掉底部的标题
		}
	});
}

function setFullScreen(Name, Height) {
	$(document).ready(function() { //DOM加载后就执行
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
	$(window).resize(function() { //改变窗体大小时     *resize()变化或者改变事件
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
	$(window).scroll(function() { //浏览器有滚动条时的操作   scroll()滚动事件
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
}